package com.spit.fammo.adapter

import android.widget.Filter
import android.widget.Filterable
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.spit.fammo.R
import com.spit.fammo.bean.db.AssetBean
import com.spit.fammo.ext.setAdapterAnimation
import com.spit.fammo.util.SettingUtil

/**
 * @Author nike
 * @Date 2023/7/7 17:05
 * @Description
 */
class AssetAdapter(data: ArrayList<AssetBean>) :
    BaseQuickAdapter<AssetBean, BaseViewHolder>(R.layout.i_asset, data), Filterable {

    init {
        setAdapterAnimation(SettingUtil.getListMode())
    }

    override fun convert(viewHolder: BaseViewHolder, bean: AssetBean) {
        mFilterList[viewHolder.layoutPosition].run {
            viewHolder.setText(R.id.tv_title,  assetno + "\n" + name)
            viewHolder.setText(R.id.tv_brand,  brand)
            viewHolder.setText(R.id.tv_model,  model)
            viewHolder.setText(R.id.tv_category,  categoryName)
            viewHolder.setText(R.id.tv_location,  spaceLoc)
            viewHolder.setText(R.id.tv_epc,  epc)

            viewHolder.setGone(R.id.tv_status,false)
            when(statusid){
                2 ->{
                    viewHolder.setBackgroundColor(R.id.tv_status, ContextCompat.getColor(context, R.color.colorPrimary))
                    viewHolder.setText(R.id.tv_status, context.getText(R.string.status_2))
                }
                3, 4 ->{
                    viewHolder.setBackgroundColor(R.id.tv_status, ContextCompat.getColor(context, R.color.holo_orange_light))
                    viewHolder.setText(R.id.tv_status, context.getText(R.string.status_3))
                }
                5, 6 ->{
                    viewHolder.setBackgroundColor(R.id.tv_status, ContextCompat.getColor(context, R.color.holo_red_dark))
                    viewHolder.setText(R.id.tv_status, context.getText(R.string.status_6))
                }
                else -> viewHolder.setGone(R.id.tv_status,true)
            }
        }
    }

    var mFilterList = ArrayList<AssetBean>()

    fun appendList(list: List<AssetBean>) {
        data = list as MutableList<AssetBean>
        //这里需要初始化filterList
        mFilterList = list as ArrayList<AssetBean>
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            //执行过滤操作
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = data as ArrayList<AssetBean>
                } else {
                    val filteredList: MutableList<AssetBean> = ArrayList()
                    for (i in data.indices) {
                        val bean = data[i]
                        if (bean.assetno?.contains(charString, ignoreCase = true) == true
                            || bean.name?.contains(charString, ignoreCase = true) == true
                            || bean.model?.contains(charString, ignoreCase = true) == true
                            || bean.brand?.contains(charString, ignoreCase = true) == true
                            || bean.epc?.contains(charString, ignoreCase = true) == true
                        ) {
                            filteredList.add(bean)
                        }
                    }
                    mFilterList = filteredList as ArrayList<AssetBean>
                }
                val filterResults = FilterResults()
                filterResults.values = mFilterList
                return filterResults
            }

            //把过滤后的值返回出来
            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                mFilterList = filterResults.values as ArrayList<AssetBean>
                notifyDataSetChanged()
            }
        }
    }

    override fun getItemCount(): Int {
        return mFilterList.size
    }

    override fun hashCode(): Int {
        return mFilterList.hashCode()
    }

}